package com.myapp.service.impl.product;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import com.myapp.model.dao.product.PictureDao;
import com.myapp.model.dao.product.ProductDao;
import com.myapp.model.entity.product.Picture;
import com.myapp.model.entity.product.Product;
import com.myapp.service.product.ProductBo;

public class ProductBoImpl implements ProductBo {

	private ProductDao prodDao;
	private PictureDao pictureDao;
	
	public void setPictureDao(PictureDao pictureDao) {
		this.pictureDao = pictureDao;
	}

	public void setProdDao(ProductDao prodDao) {
		this.prodDao = prodDao;
	}

	@Override
	public Product addProduct(Product prod){
		int oldProdId = prod.getId();
		Product p = prodDao.addProduct(prod);
		int prodId = p.getId();
		pictureDao.updateProduct(oldProdId, prodId);
		return prod;
	}
	
	public List<Product> getProduct(Set<Integer> products){
		List<Product> result = new ArrayList<Product>();
		for(int prodId : products){
			Product product = prodDao.getProduct(prodId);
			if(product != null){
				result.add(product);
			}
		}
		return result ;
	}

	@Override
	public Picture addPicture(Picture pic) {
		return pictureDao.addPicture(pic);
	}
	
	public List<Product> getLatestProducts(){
		return prodDao.getLatestProducts();
	}

	@Override
	public Product getProduct(int prodId) {
		return prodDao.getProduct(prodId);
	}
}
